/*
 * @Author: Marte
 * @Date:   2016-10-25 10:56:51
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-04 17:03:23
 */

'use strict';

var _ = require('underscore');

module.exports = function(sequelize, DataTypes) {
    var TypeRemark = sequelize.define('TypeRemark', {

    }, {
        charset: 'utf8',
        classMethods: {
            associate: function(models) {
                TypeRemark.belongsTo(models.Type, {
                    constraints: false
                });
                TypeRemark.belongsTo(models.Remark, {
                    constraints: false
                });
            },
            fetch: function(op) {
                return this
                    .findAll(_.extend({
                        order: [
                            'createdAt'
                        ],
                        raw: true
                    }, op || {}));
            },
            findRemarkByType: function(typeId) {
                var self = this;
                return self
                    .findAll({
                        where: {
                            TypeId: typeId
                        },
                        attributes: ['RemarkId'],
                        include: [{
                            association: self.associations.Remark,
                            attributes: ['remark','id']
                        }]
                    })
                    .then(function(result) {
                        return result;
                    });
            },
            findTypeByRemark: function(remarkId) {
                var self = this;
                return self
                    .findAll({
                        where: {
                            RemarkId: remarkId
                        },
                        attributes: ['TypeId'],
                        include: [{
                            association: self.associations.Type,
                            attributes: ['typeName','id']
                        }]
                    })
                    .then(function(result) {
                        return result;
                    });
            }
        }
    });
    return TypeRemark;
};
